package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.riddle.lib.model.bean.Twister;
import com.stark.riddle.lib.model.db.RiddleDaoHelperManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityTwisterBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;

public class TwisterActivity extends BaseAc<ActivityTwisterBinding> {

    //脑筋急转弯
    private int level = 0;
    private String rightProblem;
    private String rightAnswer;
    private List<Twister> listAll = new ArrayList<>();//全部数据

    @Override
    protected int onCreate() {
        return R.layout.activity_twister;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivTwisterBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.rlTwisterLast.setOnClickListener(this);
        mDataBinding.ivTwisterTip.setOnClickListener(this);
        mDataBinding.rlTwisterNext.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlTwisterLast://上一关
                if (level == 0) {
                    ToastUtils.showShort(R.string.it_is_already_the_first_level);
                    return;
                }
                level--;
                setData();
                break;
            case R.id.rlTwisterNext://下一关
                if (level == (listAll.size() - 1)) {
                    ToastUtils.showShort(R.string.it_is_already_the_last_level);
                    return;
                }
                int spLevel = SPUtil.getInt(mContext, Content.TWISTER_LEVEL, 1);
                if (level == (spLevel - 1)) {//如果通过的是未解锁关卡则解锁下一关
                    SPUtil.putInt(mContext, Content.TWISTER_LEVEL, spLevel + 1);
                }
                level++;
                setData();
                break;
            case R.id.ivTwisterTip://提手
                mDataBinding.llTwisterAnswer.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void initData() {
        level = SPUtil.getInt(mContext, Content.TWISTER_LEVEL, 1) - 1;//获取记录通过关卡数
        getLevel();
    }

    //获取关卡
    private void getLevel() {
        RiddleDaoHelperManager.getTwisterDbHelper().getByKind(Content.TWISTER_TYPE, 0, 80, new IRetCallback<List<Twister>>() {
            @Override
            public void onResult(List<Twister> result) {
                if (result != null && result.size() > 0) {
                    listAll.addAll(result);
                    setData();
                }
            }
        });
    }

    //设置数据
    private void setData() {
        //恢复选项初始状态
        mDataBinding.llTwisterAnswer.setVisibility(View.INVISIBLE);
        //设置游戏数据
        mDataBinding.tvTwisterLevel.setText(level + 1 + "/80");
        rightProblem = listAll.get(level).getTwisterDes();//问题
        rightAnswer = listAll.get(level).getTwisterKey();//正确答案
        mDataBinding.tvTwisterProblem.setText(rightProblem);
        mDataBinding.tvTwisterAnswer.setText(rightAnswer);
    }

}